(function() {
	
	if (!window.CJ) {
		window.CJ = {};
	}


	function Program () {
		this.name = 'Program';
		this.rootID = '#list';
		this.rootElem = $(this.rootID+" ul");
		this.moveDistance = -147;
		this.pageUnitCount = 4;
		this.bindEvent();
		this.searchProgramID = null;			// 2013.08.13 by SINZINET
	}
	Program.totalCount = 0;
	Program.pageSize = 50;
	Program.pageNo = 0;
	Program.setID = function (id) {
		this.lastID = id;
	};
	Program.isDataLoading = false;
	//Program.getData = function (cID, isSubCtlCall) {
	Program.getData = function (cID, isSubCtlCall, callback) {		// 2013.08.13 by SINZINET
		if (Program.isDataLoading ) {
			console.log('Program data loading....');
			return;
		}
		Program.isDataLoading = true;
		var channelID = typeof cID != 'undefined' ? cID : CJ.Channel.lastID;
		if (!!cID) {
			this.totalCount = 0;
			this.pageNo = 0;
			CJ.Scene["Program"].rootElem.html('');
			CJ.Scene["ProgramSub"].rootElem.html('');
			CJ.Channel.setID(channelID);
		}
		CJ.HttpJson.get({
			type: 'PROGRAMLIST',
			param: {
				ChannelID: channelID,
				pageSize: this.pageSize,
				pageNo: this.pageNo+1
			},
			success: function (json) {
				var initIndex = json.programlist.length > 2 ? 1 : 0;
				//Program.successCB(json);
				Program.successCB(json, callback);		// 2013.08.13 by SINZINET
				//진입시에만
				if(!!cID) {
					if (!isSubCtlCall) CJ.Scene.program();
					CJ.Episode.getData(json.programlist[initIndex].programid);
				}
			},
			complete: function () {
				Program.isDataLoading = false;
			}
		});
	};
	Program.getMore = function () {
		if (this.totalPage != this.pageNo) {
			this.getData();
		}
	};
	//Program.successCB = function (json) {
	Program.successCB = function (json, callback) {		// 2013.08.13 by SINZINET
		this.totalCount = parseInt(json.total_count);
		this.totalPage =  parseInt(json.page_info.total_page);
		this.pageNo =  parseInt(json.page_info.page);
		CJ.Scene["Program"].render(json.programlist);
		CJ.Scene["ProgramSub"].render(json.programlist);

		// 2013.08.13 by SINZINET
		if (!!callback && typeof callback == 'function') {
			callback();
		}
	};
	Program.prototype = new CJ.Vertical();
	Program.prototype.bindEvent = function (cID) {
		var self = this;
		$("#list .up").on('click', function(){
			self.pageUp();
		});
		$("#list .down").on('click', function(){
			self.pageDown();
		});
		//리스트에 mouseover
		$("#list ul").on('mouseenter', 'li', function(evt){
			$('div.btn-back>.btn').removeClass('focus');
			$('li.focus', self.rootElem).removeClass('focus');
			$(this).addClass('focus');
		}).on('mouseout', 'li', function(evt){
			$(this).removeClass('focus');
			$('li.select', self.rootElem).addClass('focus');
		});
		
		
		CJ.Vertical.prototype.bindEvent.call(this);
	};
	Program.prototype.changeEvent = function () {
		var self = this;
		$('div.btn-back>.btn').off('click').on('click', function() {
			CJ.Scene.channel();
		});
		//back 버튼에 mouseover
		$('div.btn-back>.btn').off('mouseenter').on('mouseenter', function(evt){
			$('div.btn-back>.btn').addClass('focus');
			$('li.focus',  self.rootElem).removeClass('focus');
		});
	};
	Program.prototype.render = function (lists) {
		var childCount = this.rootElem.children().length;
		var method =  childCount > 0 ? 'append' : 'html';
		$( this.rootElem )[method](
			$.render.programTemplate( lists )
		);
		if (method == 'html') {
			var initIndex = lists.length > 1 ? 1 : 0;
			var topPX = parseInt(initIndex * this.moveDistance) + 'px';
			$(this.rootID+'>h1').find('strong').html('<img src="img/tit_'+CJ.Channel.lastID+'.png" alt="">');
			$( this.rootElem ).css({top: topPX}).children('li:nth-child('+parseInt(initIndex+1)+')').addClass('focus select');
		}
		this.pageNum();
	};
	Program.prototype.pageNum = function() {
		var idx = $('li.select', this.rootElem).index();
		$(this.rootID+'>h1>b').text('('+parseInt(idx+1)+'/'+Program.totalCount+')');
	};
	Program.prototype.extend = function() {
		// 2013.08.13 by SINZINET
		this.searchProgramID = null;

		//Program 리스트에서는 이동 후 해당 episode List를 읽어와야 하기 때문에
		//이동후 Program 을 더 읽어온다
		var pID = $("li.select", this.rootElem).data("programid");
		Program.getMore();
		if (pID != Program.lastID) {
			this.pageNum();
			CJ.Episode.getData(pID);
		}
	};
	Program.prototype.setFocus = function (pID) {
		var initIndex = $( 'li[data-programid="'+pID+'"]', this.rootElem ).index();
		var topPX = parseInt(initIndex * this.moveDistance) + 'px';
		$( this.rootElem ).css({top: topPX}).children('li:nth-child('+parseInt(initIndex+1)+')').addClass('select focus').siblings().removeClass('select focus');
	};
	Program.prototype.left = function () {
		if ($('div.btn-back>.btn').hasClass('focus')) {
			this.back();
		} else {
			$("li", this.rootElem).removeClass('focus');
			$('div.btn-back>.btn').addClass('focus');
		}
	};
	Program.prototype.right = function () {
		if ($('div.btn-back>.btn').hasClass('focus')) {
			$("li.select", this.rootElem).addClass('focus');
			$('div.btn-back>.btn').removeClass('focus');
		}
	};
	Program.prototype.back = function () {
		$('div.btn-back>.btn').removeClass('focus');

		// 2013.08.13 by SINZINET
		if (!!this.searchProgramID && this.searchProgramID == Program.lastID)
			window.NetCastBack();
		else {
			this.searchProgramID = null;
			CJ.Scene.channel();
		}
	};
	Program.prototype.enter = function (evt) {
		// 2013.08.13 by SINZINET
		this.searchProgramID = null;

		if ($('div.btn-back>.btn').hasClass('focus')) {
			this.back();
		} else {
			var pID = $("li.select", this.rootElem).data("programid");
			if( Program.lastID != pID) {
				CJ.Episode.getData(pID);
			} else {
				if (CJ.Episode.lists.length>0) {
					CJ.Scene.episode();
				}
			}
		}
	};

	CJ.Program = Program;

	function ProgramSub () {
		this.name = 'ProgramSub';
		this.template = 'programSubTemplate';
		this.rootID = '#vod div.list .dep2';
		this.did = 'data-programid';
		this.rootElem = $(this.rootID+" ol");
		this.bindEvent();
	}
	ProgramSub.prototype = new CJ.SubList();
	ProgramSub.prototype.enter = function (evt) {
		this.extend();
		var pID;
		var now = $(this.rootID + ' .focus');
		if (now.hasClass('btn_prev')){
			CJ.Horizon.prototype.pageUp.call(this);
		} else if (now.hasClass('select')) {
			//컨텐츠 선택
			pID = now.data('programid');
			CJ.Scene["Program"].setFocus(pID);
			CJ.Episode.getData(pID);
			CJ.Scene.sub('EpisodeSub');
		} else if (now.hasClass('btn_next')){
			CJ.Horizon.prototype.pageDown.call(this);
		}
	};

	CJ.ProgramSub = ProgramSub;

	
	$.extend(CJ.Scene, {
							program: function() {
								var self = this;
								this["Program"].changeEvent();
								$(this["Channel"].rootID + " li.focus").removeClass('focus');
								$(this["Episode"].rootID + " li.focus").removeClass('focus');
								$(this["Program"].rootID + " li.select").addClass('focus');
								$("#wrap").removeClass().addClass(this["Program"].rootID.replace('#',''));
								this.setDelegate(this["Program"]);
							}
						});



	$(document).ready(function(){
		CJ.Scene["Program"] = new CJ.Program();
		CJ.Scene["ProgramSub"] = new CJ.ProgramSub();
	});

})();